//
//  FKSearchHistoryVC.m
//  HappyTest
//
//  Created by Macbook Pro on 2019/1/21.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKSearchHistoryVC.h"
#import "FKKeywordCell.h"
#import "FKSearchHistoryHeader.h"
#import "FKSearchKeyword.h"
#import "FKAlignmentLayout.h"
static NSString *const ID = @"FKKeywordCell";
static NSString *const HeaderID = @"FKSearchHistoryHeader";

@interface FKSearchHistoryVC ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet FKAlignmentLayout *layout;

@property (nonatomic, strong) NSArray *datas;

@end

@implementation FKSearchHistoryVC

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.layout.alignType = AlignLeft;
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 12, 0, 12);
    [self.collectionView registerClass:[FKKeywordCell class] forCellWithReuseIdentifier:ID];
    [self.collectionView registerClass:[FKSearchHistoryHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderID];
    [self loadData];
}

- (void)loadData
{
    [FKHttpTool postWithSuffix:@"m=Home&c=Index&a=getSearch" param:nil success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            NSMutableArray *datas = [NSMutableArray array];
            
            NSArray *historyItems = [FKSearchKeyword mj_objectArrayWithKeyValuesArray:result.data[@"history"]];
            if (historyItems.count > 0) {
                NSMutableDictionary *historyGroup = [NSMutableDictionary dictionary];
                [datas addObject:historyGroup];
                historyGroup[@"headerTitle"] = @"历史搜索";
                historyGroup[@"items"] = historyItems;
                historyGroup[@"showDelete"] = @(true);
            }
            
            NSArray *hotItems = [FKSearchKeyword mj_objectArrayWithKeyValuesArray:result.data[@"hot"]];
            if (hotItems.count > 0) {
                NSMutableDictionary *hotGroup = [NSMutableDictionary dictionary];
                [datas addObject:hotGroup];
                hotGroup[@"headerTitle"] = @"热门搜索";
                hotGroup[@"items"] = hotItems;
            }
            
            self.datas = datas;
            [self.collectionView reloadData];
        }
    } failure:nil];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.datas.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSDictionary *group = self.datas[section];
    NSArray *items = group[@"items"];
    return items.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FKKeywordCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    NSDictionary *group = self.datas[indexPath.section];
    NSArray *items = group[@"items"];
    cell.data = items[indexPath.item];
    return cell;
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *group = self.datas[indexPath.section];
    NSArray *items = group[@"items"];
    FKSearchKeyword *data = items[indexPath.item];
    return data.keyworkSize;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(fkScreenW, fk_adjustW(44));
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    FKSearchHistoryHeader *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderID forIndexPath:indexPath];
    NSDictionary *group = self.datas[indexPath.section];
    view.title = group[@"headerTitle"];
    view.showDelete = [group[@"showDelete"] boolValue];
    fkWeakSelf(self);
    view.deleteBlock = ^{
        [weakself fk_alertWithTitle:@"确定清除历史记录吗？" confirm:^{
            [weakself deleteSearch];
        }];
    };
    return view;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.keywordClickBlock) {
        NSDictionary *group = self.datas[indexPath.section];
        NSArray *items = group[@"items"];
        FKSearchKeyword *data = items[indexPath.item];
        self.keywordClickBlock(data.search_content);
    }
}

- (void)deleteSearch
{
    [FKHttpTool postWithSuffix:@"m=Home&c=Index&a=clear_search_history" param:nil success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            [SVProgressHUD showSuccessWithStatus:@"删除成功"];
            [self loadData];
        }
    } failure:nil];
}

@end
